home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dialog / menubox.c < prev    next >
C/C++ Source or Header  |  1996-08-01  |  8KB  |  364 lines

  1. #include "diadef.h"
  2. #include "dialog.h"
  3. #include "dialog.m"
  4.  
  5. class FIELD_MENU: public FIELD_STRING{
  6.     int column2;
  7.     char *tag;
  8.     /*~PROTOBEG~ FIELD_MENU */
  9. public:
  10.     FIELD_MENU (const char *_tag, const char *_str);
  11. protected:
  12.     void dokey (WINDOW *, int , FIELD_MSG&);
  13. public:
  14.     void drawgen (WINDOW *win, int selected);
  15.     void drawtxt (WINDOW *dialog);
  16.     void format_htmlkey (char *key, int);
  17.     const char *getmenustr (void);
  18. protected:
  19.     int getwidth1 (void);
  20. public:
  21.     void html_draw (int nof);
  22.     int html_validate (int);
  23. protected:
  24.     void restore (void);
  25.     void save (void);
  26. public:
  27.     void setcursor (WINDOW *dialog);
  28.     void setwidth1 (int _column2);
  29.     void unselect (WINDOW *dialog);
  30.     ~FIELD_MENU (void);
  31.     /*~PROTOEND~ FIELD_MENU */
  32. };
  33.  
  34. PUBLIC FIELD_MENU::FIELD_MENU(
  35.     const char *_tag,
  36.     const char *_str)
  37.     : FIELD_STRING ("",(char*)_str,60)
  38. {
  39.     tag = strdup(_tag);
  40.     setwidth1 (strlen(_tag));    // Fixed to some value until
  41.                     // setwidth1() is called, much later
  42. }
  43.  
  44. PUBLIC FIELD_MENU::~FIELD_MENU()
  45. {
  46.     free (tag);
  47. }
  48.  
  49. /*
  50.     Build a key that uniquely identify this field in the dialog
  51. */
  52. PUBLIC void FIELD_MENU::format_htmlkey(char *key, int)
  53. {
  54.     html_formatkey (key,"%s %s",tag,buf);
  55. }
  56.  
  57. /*
  58.     Fix the final disposition of the menu item
  59. */
  60. PUBLIC void FIELD_MENU::setwidth1 (int _column2)
  61. {
  62.     column2 = _column2;
  63.     box.width = _column2 + 2 + strlen(buf);
  64. }
  65. /*
  66.     Get the width of the first column.
  67. */
  68. PROTECTED int FIELD_MENU::getwidth1 ()
  69. {
  70.     return column2;
  71. }
  72. /*
  73.     Return the second string of a menu item
  74. */
  75. PUBLIC const char *FIELD_MENU::getmenustr()
  76. {
  77.     return str;
  78. }
  79.  
  80. /*
  81.     A FIELD_MENU is not really a string editor.
  82.     The following function are just there to prevent writing
  83.     over constant string.
  84. */
  85. PROTECTED void FIELD_MENU::save()
  86. {
  87. }
  88. PROTECTED void FIELD_MENU::restore()
  89. {
  90. }
  91. PROTECTED void FIELD_MENU::dokey(
  92.     WINDOW *,
  93.     int,
  94.     FIELD_MSG & )
  95. {
  96. }
  97. /*
  98.     Print menu item
  99. */
  100. PUBLIC void FIELD_MENU::drawgen(WINDOW *win, int selected)
  101. {
  102.     wattrset(win, menubox_attr);
  103.     wmove(win, box.y, box.x);
  104.     int i;
  105.     // Cleanup
  106.     for (i = 0; i < box.width; i++) waddch(win, ' ');
  107.     wmove(win, box.y, box.x);
  108.     char *pttag = tag;
  109.     if (tag[0] != ' '){
  110.         wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
  111.         waddch(win, tag[0]);
  112.         pttag++;
  113.     }
  114.     wattrset(win, selected ? tag_selected_attr : tag_attr);
  115.     waddstr(win, pttag);
  116.     wmove(win, box.y, box.x + column2 + 2);
  117.     wattrset(win, selected ? item_selected_attr : item_attr);
  118.     waddstr(win, buf);
  119.     wmove(win, box.y, box.x);
  120. }
  121.  
  122. PUBLIC void FIELD_MENU::unselect(WINDOW *dialog)
  123. {
  124.     drawgen(dialog,0);
  125. }
  126.  
  127. PUBLIC void FIELD_MENU::setcursor(WINDOW *dialog)
  128. {
  129.     drawgen (dialog,1);
  130. }
  131. PUBLIC void FIELD_MENU::drawtxt(WINDOW *dialog)
  132. {
  133.     drawgen (dialog,0);
  134. }
  135.  
  136. PUBLIC void FIELD_MENU::html_draw(int nof)
  137. {
  138.     html_printf ("<tr><td>%s<td>",tag);
  139.     char key[100];
  140.     format_htmlkey (key,nof);
  141.     html_setaref (key,buf);
  142.     html_printf ("\n");
  143. }
  144. PUBLIC int FIELD_MENU::html_validate(int)
  145. {
  146.     return 0;
  147. }
  148. /*
  149.     Evaluate the width of the first column.
  150.     Options are organisez in 2 columns.
  151. */
  152. PRIVATE void DIALOG::fixwidth1()
  153. {
  154.     int max_width1=0;
  155.     int n = getnb();
  156.     int i;
  157.     for (i=0; i<n; i++){
  158.         FIELD *f = getitem(i);
  159.         int w = f->getwidth1();
  160.         if (w > max_width1) max_width1 = w;
  161.     }
  162.     for (i=0; i<n; i++) getitem(i)->setwidth1(max_width1);
  163. }
  164.  
  165. /*
  166.     Add a new menu item to a dialog
  167.  
  168.     You will have to use the DIALOG::editmenu instead of just
  169.     DIALOG::edit() or your dialog will lack some buttons and won't
  170.     be layout properly.
  171. */
  172. PUBLIC void DIALOG::new_menuitem (const char *prompt1, const char *prompt2)
  173. {
  174.     if (strcmp(prompt1,"-")==0){
  175.         /* #Specification: dialog / menus / splitter
  176.             If the first member of a menu entry
  177.             is a single '-', this entry is
  178.             drawn as a full splitter line
  179.  
  180.             The second entry will be used as the
  181.             splitter title.
  182.         */
  183.         newf_title ("",prompt2);
  184.     }else{
  185.         if (prompt1[0] == '\0') prompt1 = " ";
  186.         FIELD_MENU *men = new FIELD_MENU (prompt1,prompt2);
  187.         add (men);
  188.     }
  189. }
  190. /*
  191.     Add a new menu item to a dialog
  192.  
  193.     You will have to use the DIALOG::editmenu instead of just
  194.     DIALOG::edit() or your dialog will lack some buttons and won't
  195.     be layout properly.
  196. */
  197. PUBLIC void DIALOG::new_menuitem (const char *prompt1, const SSTRING &prompt2)
  198. {
  199.     new_menuitem (prompt1,prompt2.get());
  200. }
  201. PUBLIC void DIALOG::new_menuitem (const SSTRING &prompt1, const SSTRING &prompt2)
  202. {
  203.     new_menuitem (prompt1.get(),prompt2);
  204. }
  205.  
  206. /*
  207.     Return the second string of a menu item
  208. */
  209. PUBLIC const char *DIALOG::getmenustr(int choice)
  210. {
  211.     const char *ret = NULL;
  212.     if (choice >=0 && choice < getnb()){
  213.         ret = getitem(choice)->getmenustr();
  214.     }
  215.     return ret;
  216. }
  217.  
  218. /*
  219.     Record a small help for the Save button
  220. */
  221. PUBLIC void DIALOG::savewhat (const char *help)
  222. {
  223.     what.save.setfrom (help);
  224. }
  225. /*
  226.     Record a small help for the Del button
  227. */
  228. PUBLIC void DIALOG::delwhat (const char *help)
  229. {
  230.     what.del.setfrom (help);
  231. }
  232. /*
  233.     Record a small help for the Ins button
  234. */
  235. PUBLIC void DIALOG::inswhat (const char *help)
  236. {
  237.     what.ins.setfrom (help);
  238. }
  239. /*
  240.     Record a small help for the Add button
  241. */
  242. PUBLIC void DIALOG::addwhat (const char *help)
  243. {
  244.     what.add.setfrom (help);
  245. }
  246. static void append_what (
  247.     int &options,
  248.     int optval,
  249.     SSTRING &str,
  250.     const char *msg,
  251.     SSTRING &explan)
  252. {
  253.     if (!str.is_empty()){
  254.         char buf[100];
  255.         sprintf (buf,MSG_U(I_SELECT
  256.             ,"\nSelect <%s> to %s %s"),msg,msg,str.get());
  257.         options |= optval;
  258.         explan.append (buf);
  259.     }
  260. }
  261.  
  262. /*
  263.     Edit a menu
  264. */
  265. PUBLIC MENU_STATUS DIALOG::editmenu(
  266.     const char *title,
  267.     const char *prompt,
  268.     const char *helpfile,
  269.     int &sel,    // Will hold the selection or -1 if escape
  270.             // It must already contains the initial selection
  271.     int  options)    // MENUBUT_ADD | MENUBUT_INS | MENUBUT_DEL | MENUBUT_SAVE
  272. {
  273.     SSTRING tmp_explan (prompt);
  274.     int spc_options = 0;
  275.     append_what (spc_options,MENUBUT_SAVE,what.save
  276.         ,MSG_U(I_SAVE,"save"),tmp_explan);
  277.     append_what (spc_options,MENUBUT_ADD,what.add
  278.         ,MSG_U(I_ADD,"add"),tmp_explan);
  279.     append_what (spc_options,MENUBUT_INS,what.ins
  280.         ,MSG_U(I_INS,"ins"),tmp_explan);
  281.     append_what (spc_options,MENUBUT_DEL,what.del
  282.         ,MSG_U(I_DEL,"del"),tmp_explan);
  283.     fixwidth1();
  284.     return edit (title,tmp_explan.get(),helpfile,sel
  285.         ,options|spc_options|MENUBUT_OK|MENUBUT_QUIT);
  286. }
  287.  
  288. PUBLIC MENU_STATUS DIALOG::editmenu(
  289.     const char *title,
  290.     const char *prompt,
  291.     HELP_FILE &helpfile,
  292.     int &sel,    // Will hold the selection or -1 if escape
  293.             // It must already contains the initial selection
  294.     int  options)    // MENUBUT_ADD | MENUBUT_INS | MENUBUT_DEL | MENUBUT_SAVE
  295. {
  296.     return editmenu (title,prompt,helpfile.getpath(),sel,options);
  297. }
  298.  
  299. /*
  300.  * Display a menu for choosing among a number of options
  301.  * Return    MENU_OK, MENU_QUIT or MENU_ESCAPE
  302.  * Depending on the options parameter, may return MENU_INS, MENU_SAVE
  303.  *     MENU_ADD or MENU_DEL.
  304.  */
  305. MENU_STATUS dialog_menu(
  306.     const char *title,
  307.     const char *prompt,
  308.     const char *helpfile,
  309.     int  options,    // MENUBUT_ADD | MENUBUT_INS | MENUBUT_DEL | MENUBUT_SAVE
  310.     int item_no,
  311.     char **items,
  312.     int &sel)    // Will hold the selection or -1 if escape
  313.             // It must already contains the initial selection
  314. {
  315.     DIALOG dia;
  316.     for (int i=0; i<item_no; i++){
  317.         dia.new_menuitem(items[i*2],items[i*2+1]);
  318.     }
  319.     return dia.editmenu (title,prompt,helpfile,sel,options);
  320. }
  321.  
  322.  
  323. #ifdef TEST
  324.  
  325. int main (int argc, char *argv[])
  326. {
  327.     int ret,choice=0;
  328.     static char *tb[]={
  329.         "a",    "message",
  330.         "allo",    "alal",
  331.         "b",    "bbbbbb",
  332.         "bozo",    "bzbzbzbz",
  333.         "c",    "coco",
  334.         "coco",    "cocococococ",
  335.         "d",    "dddd",
  336.         "dozo",    "dozozododo",
  337.     };
  338.     init_dialog();
  339.     if (argc == 2){
  340.         dialog_settimeout(atoi(argv[1]), '\n');
  341.     }
  342.     ret  = dialog_menu(
  343.         "This is a test"
  344.         ,"Are you\nsure you want to exit"
  345.         ,"/tmp/dialog.bak"
  346.         ,MENUBUT_SAVE
  347.         ,8,tb,choice);
  348.     ret  = dialog_menu(
  349.         "This is a test"
  350.         ,"Are you\nsure you want to exit\n"
  351.         ,"/tmp/dialog.bak"
  352.         ,MENUBUT_SAVE | MENUBUT_ADD | MENUBUT_DEL
  353.         ,8,tb,choice);
  354.  
  355.  
  356.     endwin();
  357.     printf ("ret = %d choice = %d\n",ret,choice);
  358.     return 0;
  359. }
  360.  
  361. #endif
  362.  
  363.  
  364.